use super::Project;
use crate::prequire::BinaryCommand;
use crate::prequire::adb::{
  collect::CollectCmd,
  connect::{ConnectCmd, DisconnectCmd},
  DeviceTypes,
};
use crate::validate_socket;
use clap::Subcommand;


#[derive(Debug, Subcommand)]
pub enum ProjectDeviceCommands {
  Connect {
    #[arg(
      short,
      long,
      value_name = "TYPE",
      require_equals = true,
      default_value_t = DeviceTypes::Android,
      default_missing_value = "android",
      value_enum
    )]
    device_type: DeviceTypes,

    #[arg(require_equals = true, value_parser = validate_socket)]
    socket: String,
  },
  Disconnect {
    #[arg(value_parser = validate_socket)]
    socket: Option<String>,
  },
}

pub fn serve_commands(proj: &mut Project, command: &ProjectDeviceCommands) {
  match command {
    ProjectDeviceCommands::Connect {
      device_type,
      socket,
    } => {
      println!("device connecting <{device_type:?}>device to {socket}");
      ConnectCmd::new(socket, None).exec();
      let mut specs = CollectCmd::new().exec();
      proj.apply_to_device(&mut specs);
    }
    ProjectDeviceCommands::Disconnect { socket } => {
      println!("device disconnecting {:?}", socket);
      DisconnectCmd::new(socket.as_ref().map_or(None, |v| Some(&*v)), None).exec();
    }
  }
}